package com.amarsoft.are.lang;

import java.io.Serializable;
import java.util.Date;
import java.util.Properties;

public class DataElement
  implements Cloneable, Comparable<Object>, Serializable, Element
{
  private static final long serialVersionUID = 1L;
  public static final byte STRING = 0;
  public static final byte INT = 1;
  public static final byte LONG = 2;
  public static final byte DOUBLE = 4;
  public static final byte BOOLEAN = 8;
  public static final byte DATE = 16;
  private String id = null;
  private String name;
  private byte type;
  private String label;
  private int length;
  private int scale;
  private Properties extendProperties;
  private int intValue;
  private double doubleValue;
  private Date dateValue;
  private String stringValue;
  private boolean booleanValue;
  private long longValue;
  private boolean isNull = true;
  private boolean converted;
  private byte setType = 0;
  private static int unnamedCount = 0;
  private static long idCount = 0L;

  public static final int unnamedInstanceCount()
  {
    return unnamedCount;
  }

  public DataElement()
  {
    this.type = 0;
    this.name = "de_" + String.valueOf(++unnamedCount);
    setNull();
  }

  public DataElement(String paramString)
  {
    this.type = 0;
    this.name = "de_" + String.valueOf(++unnamedCount);
    setNull();
  }

  public DataElement(String paramString, byte paramByte)
  {
    this.name = "de_" + String.valueOf(++unnamedCount);
    this.type = paramByte;
    setNull();
  }

  public String getId()
  {
    if (this.id == null)
      this.id = String.valueOf(idCount++);
    return this.id;
  }

  public final String getName()
  {
    return this.name;
  }

  public final String getLabel()
  {
    return ((this.label == null) ? this.name : this.label);
  }

  public final void setLabel(String paramString)
  {
    this.label = paramString;
  }

  public int getLength()
  {
    return this.length;
  }

  public void setLength(int paramInt)
  {
    this.length = paramInt;
  }

  public int getScale()
  {
    return this.scale;
  }

  public void setScale(int paramInt)
  {
    this.scale = paramInt;
  }

  public final Date getDate()
  {
    if ((!(this.converted)) && (this.setType != 16))
    {
      typeConvert();
      this.converted = true;
    }
    return this.dateValue;
  }

  public final boolean isNull()
  {
    return this.isNull;
  }

  public final void setNull()
  {
    this.intValue = 0;
    this.doubleValue = 0.0D;
    this.stringValue = "";
    this.dateValue = new Date(0L);
    this.booleanValue = false;
    this.longValue = 0L;
    this.converted = true;
    this.isNull = true;
  }

  public final double getDouble()
  {
    if ((!(this.converted)) && (this.setType != 4))
    {
      typeConvert();
      this.converted = true;
    }
    return this.doubleValue;
  }

  public final String getString()
  {
    if ((!(this.converted)) && (this.setType != 0))
    {
      typeConvert();
      this.converted = true;
    }
    return this.stringValue;
  }

  public final int getInt()
  {
    if ((!(this.converted)) && (this.setType != 1))
    {
      typeConvert();
      this.converted = true;
    }
    return this.intValue;
  }

  public final long getLong()
  {
    if ((!(this.converted)) && (this.setType != 2))
    {
      typeConvert();
      this.converted = true;
    }
    return this.longValue;
  }

  public final boolean getBoolean()
  {
    if ((!(this.converted)) && (this.setType != 8))
    {
      typeConvert();
      this.converted = true;
    }
    return this.booleanValue;
  }

  public final String toString()
  {
    return getString();
  }

  public Object clone()
  {
    DataElement localDataElement = null;
    try
    {
      localDataElement = (DataElement)super.clone();
    }
    catch (CloneNotSupportedException localCloneNotSupportedException)
    {
    }
    return localDataElement;
  }

  public final byte getType()
  {
    return this.type;
  }

  public final void setValue(String paramString)
  {
    this.stringValue = paramString;
    this.isNull = (paramString == null);
    this.setType = 0;
    this.converted = false;
  }

  public final void setValue(int paramInt)
  {
    this.intValue = paramInt;
    this.isNull = false;
    this.setType = 1;
    this.converted = false;
  }

  public final void setValue(long paramLong)
  {
    this.longValue = paramLong;
    this.isNull = false;
    this.setType = 2;
    this.converted = false;
  }

  public final void setValue(double paramDouble)
  {
    this.doubleValue = paramDouble;
    this.isNull = false;
    this.setType = 4;
    this.converted = false;
  }

  public final void setValue(boolean paramBoolean)
  {
    this.booleanValue = paramBoolean;
    this.isNull = false;
    this.setType = 8;
    this.converted = false;
  }

  public final void setValue(Date paramDate)
  {
    this.dateValue = paramDate;
    this.isNull = (paramDate == null);
    this.setType = 16;
    this.converted = false;
  }

  public final void setValue(DataElement paramDataElement)
  {
    if ((paramDataElement == null) || (paramDataElement.isNull()))
    {
      setNull();
      return;
    }
    switch (this.type)
    {
    case 0:
      setValue(paramDataElement.getString());
      break;
    case 1:
      setValue(paramDataElement.getInt());
      break;
    case 2:
      setValue(paramDataElement.getLong());
      break;
    case 4:
      setValue(paramDataElement.getDouble());
      break;
    case 8:
      setValue(paramDataElement.getBoolean());
      break;
    case 16:
      setValue(paramDataElement.getDate());
      break;
    case 3:
    case 5:
    case 6:
    case 7:
    case 9:
    case 10:
    case 11:
    case 12:
    case 13:
    case 14:
    case 15:
    default:
      setValue(paramDataElement.getString());
    }
  }

  public final Object getValue()
  {
    Object localObject = null;
    switch (this.type)
    {
    case 0:
      localObject = getString();
      break;
    case 1:
      localObject = new Integer(getInt());
      break;
    case 2:
      localObject = new Long(getLong());
      break;
    case 4:
      localObject = new Double(getDouble());
      break;
    case 8:
      localObject = new Boolean(getBoolean());
      break;
    case 16:
      localObject = getDate();
      break;
    case 3:
    case 5:
    case 6:
    case 7:
    case 9:
    case 10:
    case 11:
    case 12:
    case 13:
    case 14:
    case 15:
    default:
      localObject = getString();
    }
    return localObject;
  }

  public final void setValue(Object paramObject)
  {
    if (paramObject == null)
    {
      setNull();
      return;
    }
    if (paramObject instanceof String)
      setValue((String)paramObject);
    else if (paramObject instanceof Integer)
      setValue(((Integer)paramObject).intValue());
    else if (paramObject instanceof Long)
      setValue(((Long)paramObject).longValue());
    else if (paramObject instanceof Double)
      setValue(((Double)paramObject).doubleValue());
    else if (paramObject instanceof Boolean)
      setValue(((Boolean)paramObject).booleanValue());
    else if (paramObject instanceof Date)
      setValue((Date)paramObject);
    else if (paramObject instanceof DateX)
      setValue(((DateX)paramObject).getTime());
    else if (paramObject instanceof Element)
      setValue(((Element)paramObject).getValue());
    else
      setValue(paramObject.toString());
  }

  private final void typeConvert()
  {
    if (this.isNull)
    {
      setNull();
      return;
    }
    switch (this.setType)
    {
    case 0:
      string2other();
      break;
    case 1:
      this.stringValue = String.valueOf(this.intValue);
      this.longValue = this.intValue;
      this.doubleValue = this.intValue;
      this.booleanValue = (this.intValue != 0);
      this.dateValue = new Date(this.intValue);
      break;
    case 2:
      this.stringValue = String.valueOf(this.longValue);
      this.intValue = (int)this.longValue;
      this.doubleValue = this.longValue;
      this.booleanValue = (this.longValue != 0L);
      this.dateValue = new Date(this.longValue);
      break;
    case 4:
      this.stringValue = String.valueOf(this.doubleValue);
      this.intValue = (int)this.doubleValue;
      this.longValue = (int)this.doubleValue;
      this.booleanValue = (this.doubleValue != 0.0D);
      this.dateValue = new Date(this.longValue);
      break;
    case 8:
      this.stringValue = String.valueOf(this.booleanValue);
      this.intValue = ((this.booleanValue) ? 1 : 0);
      this.longValue = this.intValue;
      this.doubleValue = this.intValue;
      this.dateValue = new Date(this.longValue);
      break;
    case 16:
      this.stringValue = DateX.format(this.dateValue);
      this.longValue = this.dateValue.getTime();
      this.intValue = (int)this.longValue;
      this.doubleValue = this.longValue;
      this.booleanValue = (this.longValue != 0L);
      break;
    case 3:
    case 5:
    case 6:
    case 7:
    case 9:
    case 10:
    case 11:
    case 12:
    case 13:
    case 14:
    case 15:
    default:
      string2other();
    }
  }

  private final void string2other()
  {
    switch (this.type)
    {
    case 16:
      this.dateValue = StringX.parseDate(this.stringValue);
      if (this.dateValue == null)
      {
        try
        {
          Double localDouble1 = Double.valueOf(this.stringValue.replaceAll(",", ""));
          this.intValue = localDouble1.intValue();
          this.longValue = localDouble1.longValue();
          this.doubleValue = localDouble1.doubleValue();
          this.dateValue = new Date(this.longValue);
        }
        catch (NumberFormatException localNumberFormatException1)
        {
          this.intValue = 0;
          this.longValue = 0L;
          this.doubleValue = 0.0D;
          this.dateValue = null;
        }
      }
      else
      {
        this.longValue = this.dateValue.getTime();
        this.intValue = (int)this.longValue;
        this.doubleValue = this.longValue;
      }
      this.booleanValue = (this.dateValue != null);
      break;
    case 1:
    case 2:
    case 4:
      try
      {
        Double localDouble2 = Double.valueOf(this.stringValue.replaceAll(",", ""));
        this.intValue = localDouble2.intValue();
        this.longValue = localDouble2.longValue();
        this.doubleValue = localDouble2.doubleValue();
        this.dateValue = new Date(this.longValue);
      }
      catch (NumberFormatException localNumberFormatException2)
      {
        this.intValue = 0;
        this.longValue = 0L;
        this.doubleValue = 0.0D;
        this.dateValue = null;
      }
      this.booleanValue = (this.intValue != 0);
      break;
    case 0:
      this.booleanValue = StringX.parseBoolean(this.stringValue);
      this.dateValue = StringX.parseDate(this.stringValue);
      try
      {
        Double localDouble3 = Double.valueOf(this.stringValue.replaceAll(",", ""));
        this.intValue = localDouble3.intValue();
        this.longValue = localDouble3.longValue();
        this.doubleValue = localDouble3.doubleValue();
        if (this.dateValue == null)
          this.dateValue = new Date(this.longValue);
      }
      catch (NumberFormatException localNumberFormatException3)
      {
        if (this.dateValue != null)
        {
          this.longValue = this.dateValue.getTime();
          this.intValue = (int)this.longValue;
          this.doubleValue = this.longValue;
        }
        else
        {
          this.longValue = 0L;
          this.intValue = 0;
          this.doubleValue = 0.0D;
        }
      }
      break;
    case 8:
      this.booleanValue = StringX.parseBoolean(this.stringValue);
      this.dateValue = StringX.parseDate(this.stringValue);
      try
      {
        Double localDouble4 = Double.valueOf(this.stringValue.replaceAll(",", ""));
        this.intValue = localDouble4.intValue();
        this.longValue = localDouble4.longValue();
        this.doubleValue = localDouble4.doubleValue();
        if (this.dateValue == null)
          this.dateValue = new Date(this.longValue);
      }
      catch (NumberFormatException localNumberFormatException4)
      {
        this.intValue = ((this.booleanValue) ? 1 : 0);
        this.longValue = this.intValue;
        this.doubleValue = this.intValue;
      }
    case 3:
    case 5:
    case 6:
    case 7:
    case 9:
    case 10:
    case 11:
    case 12:
    case 13:
    case 14:
    case 15:
    }
  }

  public int compareTo(Object paramObject)
  {
    if (!(paramObject instanceof DataElement))
    {
      if (isNull())
        return -1;
      return getString().compareTo(paramObject.toString());
    }
    DataElement localDataElement = (DataElement)paramObject;
    if ((isNull()) && (localDataElement.isNull()))
      return 0;
    if ((isNull()) && (!(localDataElement.isNull())))
      return -2147483648;
    if ((!(isNull())) && (localDataElement.isNull()))
      return 2147483647;
    if (this.type != localDataElement.type)
      return getString().compareTo(localDataElement.getString());
    int i = 0;
    switch (this.type)
    {
    case 1:
      int j = getInt();
      int k = localDataElement.getInt();
      if (j > k)
      {
        i = 1;
//        break label331:
      }
      if (j < k)
      {
        i = -1;
//        break label331:
      }
      i = 0;
      break;
    case 2:
      long l1 = getLong();
      long l2 = localDataElement.getLong();
      if (l1 > l2)
      {
        i = 1;
//        break label331:
      }
      if (l1 < l2)
      {
        i = -1;
//        break label331:
      }
      i = 0;
      break;
    case 4:
      double d1 = getDouble();
      double d2 = localDataElement.getDouble();
      if (d1 > d2)
      {
        i = 1;
//        break label331:
      }
      if (d1 < d2)
      {
        i = -1;
//        break label331:
      }
      i = 0;
      break;
    case 8:
      i = getInt() - localDataElement.getInt();
      break;
    case 16:
      i = getDate().compareTo(localDataElement.getDate());
      break;
    default:
      i = getString().compareTo(localDataElement.getString());
    }
    return i;
  }

  public boolean equals(Object paramObject)
  {
    return (compareTo(paramObject) == 0);
  }

  public String getProperty(String paramString)
  {
    if ((this.extendProperties == null) || (paramString == null))
      return null;
    return this.extendProperties.getProperty(paramString);
  }

  public void setProperty(String paramString1, String paramString2)
  {
    if (this.extendProperties == null)
      this.extendProperties = new Properties();
    this.extendProperties.setProperty(paramString1, paramString2);
  }

  public String[] getProperties()
  {
    String[] arrayOfString = new String[0];
    if (this.extendProperties != null)
      arrayOfString = (String[])this.extendProperties.keySet().toArray(arrayOfString);
    return arrayOfString;
  }

  public static DataElement valueOf(String paramString, int paramInt)
  {
    DataElement localDataElement = new DataElement(paramString, (byte)1);
    localDataElement.setValue(paramInt);
    return localDataElement;
  }

  public static DataElement valueOf(String paramString, long paramLong)
  {
    DataElement localDataElement = new DataElement(paramString, (byte)2);
    localDataElement.setValue(paramLong);
    return localDataElement;
  }

  public static DataElement valueOf(String paramString, double paramDouble)
  {
    DataElement localDataElement = new DataElement(paramString, (byte)4);
    localDataElement.setValue(paramDouble);
    return localDataElement;
  }

  public static DataElement valueOf(String paramString, Date paramDate)
  {
    DataElement localDataElement = new DataElement(paramString, (byte)16);
    localDataElement.setValue(paramDate);
    return localDataElement;
  }

  public static DataElement valueOf(String paramString, boolean paramBoolean)
  {
    DataElement localDataElement = new DataElement(paramString, (byte)8);
    localDataElement.setValue(paramBoolean);
    return localDataElement;
  }

  public static DataElement valueOf(String paramString1, String paramString2)
  {
    DataElement localDataElement = new DataElement(paramString1);
    localDataElement.setValue(paramString2);
    return localDataElement;
  }

  public static DataElement valueOf(int paramInt)
  {
    return valueOf(null, paramInt);
  }

  public static DataElement valueOf(long paramLong)
  {
    return valueOf(null, paramLong);
  }

  public static DataElement valueOf(double paramDouble)
  {
    return valueOf(null, paramDouble);
  }

  public static DataElement valueOf(Date paramDate)
  {
    return valueOf(null, paramDate);
  }

  public static DataElement valueOf(String paramString)
  {
    return valueOf(null, paramString);
  }

  public static DataElement valueOf(boolean paramBoolean)
  {
    return valueOf(null, paramBoolean);
  }
}